<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="110px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="名称" prop="name">
						<el-input v-model="form.name" placeholder="请输入名称" />
					</el-form-item>
				</el-col>

				<!-- <el-col :span="12" class="mb20">
					<el-form-item label="监测点类型" prop="monitorType">
						<el-select v-model="form.monitorType" placeholder="请选择监测点类型" @change="handleTypeChange">
							<el-option :value="0" label="微型网格点"></el-option>
							<el-option :value="1" label="微型VOC站"></el-option>
						</el-select>
					</el-form-item>
				</el-col> -->

				<el-col :span="12" class="mb20">
					<el-form-item label="经度" prop="longitude">
						<el-input v-model="form.longitude" placeholder="请输入经度">
							<template #append>
								<div @click="handleMap" class="cursor-pointer">选择</div>
							</template>
						</el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="纬度" prop="latitude">
						<el-input v-model="form.latitude" placeholder="请输入纬度">
							<template #append>
								<div @click="handleMap" class="cursor-pointer">选择</div>
							</template>
						</el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="区域名称" prop="areaName">
						<el-input v-model="form.areaName" placeholder="请输入区域名称" readonly />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="负责管理员" prop="pid">
						<el-select v-model="form.pid" placeholder="请选择管理员" @change="handleGridChange">
							<el-option v-for="item in gridOptions" :key="item.value" :value="item.value" :label="item.label"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
	<!-- 地图 -->
	<el-dialog title="选择地图位置" v-model="visibleMap" :close-on-click-modal="false" draggable destroy-on-close>
		<MapArea class="h-[600px] w-full" @select="handleSelect" :latitude="form.latitude" :longitude="form.longitude" />
	</el-dialog>
</template>

<script setup lang="ts" name="AirSiteDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/platform/airSite';
import { rule } from '/@/utils/validate';
import { fetchList } from '/@/api/platform/airGridMember';
import MapArea from './components/map.vue';

interface optionTypes {
	label: string;
	value: string | number;
}
const emit = defineEmits(['refresh']);
const props = defineProps({
	areaName: {
		type: String,
		default: '',
	},
	areaId: {
		type: String,
		default: '',
	},
});

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const visibleMap = ref(false);
const loading = ref(false);
const gridOptions = ref<optionTypes[]>([]);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	name: '',
	monitorTypeName: '',
	// monitorType: '',
	areaName: '',
	areaId: '',
	longitude: undefined,
	latitude: undefined,
	pname: null,
	pid: '',
});

// 定义校验规则
const dataRules = ref({
	name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
	// monitorType: [{ required: true, message: '监测类型不能为空', trigger: 'change' }],
	longitude: [{ required: true, message: '经度不能为空', trigger: 'blur' }],
	latitude: [{ required: true, message: '纬度不能为空', trigger: 'blur' }],
	pid: [{ required: true, message: '网格员不能为空', trigger: 'change' }],
});
// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
		form.areaName = props.areaName;
		form.areaId = props.areaId;
	});
	getSiteNameOptions(props.areaId);
	// 获取airSite信息
	if (id) {
		form.id = id;
		getairSiteData(id);
	}
};

const handleTypeChange = (e: number) => {
	if (e === 0) {
		form.monitorTypeName = '微型网格点';
	} else {
		form.monitorTypeName = '微型VOC站';
	}
};

const handleGridChange = (e: any) => {
	const item: any = unref(gridOptions)?.find((d: any) => d.value === e);
	form.pname = item.label;
};
// 获取网格员
const getSiteNameOptions = (tag: any) => {
	fetchList({ tag }).then((res) => {
		gridOptions.value = res.data.records.map((d: any) => {
			return {
				label: d.name,
				value: d.id,
			};
		});
	});
};
// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getairSiteData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};
const handleMap = () => {
	if (!visibleMap.value) {
		visibleMap.value = true;
	}
};
// 地图位置
const handleSelect = (data: any) => {
	form.latitude = data[1];
	form.longitude = data[0];
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
